package com.yy.community.modules.article.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.community.common.utils.PageUtils;
import com.yy.community.common.utils.Query;
import com.yy.community.modules.article.entity.Article;
import com.yy.community.modules.article.mapper.ArticleMapper;
import com.yy.community.modules.article.service.IArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.community.modules.comment.entity.Comment;
import com.yy.community.modules.comment.service.ICommentService;
import com.yy.community.modules.entity.article.vo.ArticleVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 司马缸砸缸了
 * @since 2019-07-12
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Autowired
    private ICommentService commentService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String authorId = (String)params.get("authorId");
        String title = (String)params.get("title");
        String category = (String)params.get("category");
        Integer isTop = (Integer)params.get("isTop");

        Page<Article> queryPage = new Query<Article>(params).getPage();
        IPage<Article> page = baseMapper.selectPage(queryPage, Wrappers.<Article>lambdaQuery()
                .eq(StringUtils.isNotBlank(authorId), Article::getAuthorId, authorId)
                .eq(StringUtils.isNotBlank(category), Article::getCategory, category)
                .eq(isTop != null, Article::getIsTop, isTop)
                .like(StringUtils.isNotBlank(title), Article::getTitle, title).orderByAsc(Article::getCreateTime));

        return new PageUtils(page);
    }

    @Override
    public List<Article> queryList(Article article) {
        List<Article> list = baseMapper.selectList(Wrappers.<Article>lambdaQuery()
            .eq(StringUtils.isNotBlank(article.getAuthorId()), Article::getAuthorId, article.getAuthorId())
            .eq(StringUtils.isNotBlank(article.getAuthorName()), Article::getAuthorName, article.getAuthorName())
            .eq(StringUtils.isNotBlank(article.getCategory()), Article::getCategory, article.getCategory())
            .eq(article.getIsTop() != null, Article::getIsTop, article.getIsTop())
            .like(StringUtils.isNotBlank(article.getTitle()), Article::getTitle, article.getTitle())
            .orderByDesc(Article::getCreateTime)
        );

        return list;
    }

    @Override
    public Article queryById(String id) {
        Article article = this.getById(id);
        ArticleVo vo = new ArticleVo();
        BeanUtil.copyProperties(article, vo);
        // 查询所有一级评论
        List<Comment> commentList = commentService.list(Wrappers.<Comment>lambdaQuery()
                .eq(Comment::getArticleId, id)
                .eq(Comment::getCommentLevel, 1)
        );
        vo.setCommentList(commentList);

        return vo;
    }

    @Override
    public void updateArticleById(Article article) {
        baseMapper.updateById(article);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteArticleById(String id) {
        baseMapper.deleteById(id);
        // 删除评论
        commentService.deleteComment(new Comment().setArticleId(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(String[] ids) {
        //先删除笔记标签多对多关联
        Optional.ofNullable(ids).ifPresent(idList ->
                Arrays.stream(idList).forEach(id -> deleteArticleById(id))
        );
    }
}
